use tauri::menu::{Menu, MenuItem, PredefinedMenuItem, Submenu};
use tauri::tray::TrayIconBuilder;
use tauri::window::Color;
use tauri::{AppHandle, Emitter, Manager, Result, Runtime, WebviewUrl, WebviewWindowBuilder};

// pub fn create(app: &AppHandle) -> Result<()> {
//     let _ = TrayIconBuilder::new().build(app)?;
//     Ok(())
// }

/**
 * 创建 Webview 窗口的函数
 * Used to create a new Webview window
 */
fn create_webview_window<R: Runtime>(
    app: &AppHandle<R>,
    label: &str,
    url: &str,
    title: &str,
) -> tauri::Result<()> {
    WebviewWindowBuilder::new(app, label, WebviewUrl::App(url.into()))
        .decorations(false)
        .title(title)
        .transparent(true)
        .always_on_top(true)
        .background_color(Color(0, 0, 0, 0))
        .build()
        .map(|_| ())
        .map_err(|e| {
            eprintln!("Failed to create window: {}", e);
            e
        })
}

pub fn create(app: &AppHandle) -> Result<()> {
    let menu = create_menu(app)?;

    let _ = TrayIconBuilder::with_id("tray")
        .tooltip("hClock")
        .icon(app.default_window_icon().unwrap().clone())
        .menu_on_left_click(true)
        .menu(&menu)
        .on_menu_event(move |app, event| handle_menu_event(app, event.id.as_ref()))
        .build(app);
    Ok(())
}

fn create_menu<R: Runtime>(app: &AppHandle<R>) -> Result<Menu<R>> {
    // Separator item
    let separator = PredefinedMenuItem::separator(app)?;

    // menu items
    let quit_i = MenuItem::with_id(app, "quit", "退出", true, None::<&str>)?;
    // let new_window_i = MenuItem::with_id(app, "new-window", "新窗口", true, None::<&str>)?;
    let help_i = MenuItem::with_id(app, "help-menu", "帮助", true, None::<&str>)?;
    let about_i = MenuItem::with_id(app, "about-menu", "关于", true, None::<&str>)?;

    // sum menu item
    let show_i = MenuItem::with_id(app, "show", "显示", true, None::<&str>)?;
    let hide_i = MenuItem::with_id(app, "hide", "隐藏", true, None::<&str>)?;
    let toggle_submenu = Submenu::with_items(app, "窗口切换", true, &[&show_i, &hide_i])?;

    // font size item
    let font_14px = MenuItem::with_id(app, "font-14px", "14px", true, None::<&str>)?;
    let font_16px = MenuItem::with_id(app, "font-16px", "16px", true, None::<&str>)?;
    let font_18px = MenuItem::with_id(app, "font-18px", "18px", true, None::<&str>)?;
    let font_20px = MenuItem::with_id(app, "font-20px", "20px", true, None::<&str>)?;
    let font_24px = MenuItem::with_id(app, "font-24px", "24px", true, None::<&str>)?;
    let font_36px = MenuItem::with_id(app, "font-36px", "36px", true, None::<&str>)?;
    let font_48px = MenuItem::with_id(app, "font-48px", "48px", true, None::<&str>)?;
    let font_60px = MenuItem::with_id(app, "font-60px", "60px", true, None::<&str>)?;
    let font_72px = MenuItem::with_id(app, "font-72px", "72px", true, None::<&str>)?;
    let font_size_submenu = Submenu::with_items(
        app,
        "字体大小",
        true,
        &[
            &font_14px, &font_16px, &font_18px, &font_20px, &font_24px, &font_36px, &font_48px,
            &font_60px, &font_72px,
        ],
    )?;

    // font color item
    let font_ff4a58 = MenuItem::with_id(app, "font-ff4a58", "茶花红", true, None::<&str>)?;
    let font_f7880d = MenuItem::with_id(app, "font-f7880d", "芒果橙", true, None::<&str>)?;
    let font_ebb10d = MenuItem::with_id(app, "font-ebb10d", "栀子黄", true, None::<&str>)?;
    let font_6dd400 = MenuItem::with_id(app, "font-6dd400", "青草绿", true, None::<&str>)?;
    let font_05d892 = MenuItem::with_id(app, "font-05d892", "碧玉青", true, None::<&str>)?;
    let font_158aff = MenuItem::with_id(app, "font-158aff", "宝石蓝", true, None::<&str>)?;
    let font_8b0dcb = MenuItem::with_id(app, "font-8b0dcb", "青莲紫", true, None::<&str>)?;
    let font_color_submenu = Submenu::with_items(
        app,
        "字体颜色",
        true,
        &[
            &font_ff4a58,
            &font_f7880d,
            &font_ebb10d,
            &font_6dd400,
            &font_05d892,
            &font_158aff,
            &font_8b0dcb,
        ],
    )?;

    // font family item
    // const fontFamilies = ['Arial', 'Helvetica', 'Georgia', 'Times New Roman', 'Courier New', 'Console']
    let font_arial = MenuItem::with_id(app, "font-arial", "Arial", true, None::<&str>)?;
    let font_helvetica = MenuItem::with_id(app, "font-helvetica", "Helvetica", true, None::<&str>)?;
    let font_georgia = MenuItem::with_id(app, "font-georgia", "Georgia", true, None::<&str>)?;
    let font_times_new_roman = MenuItem::with_id(
        app,
        "font-times-new-roman",
        "Times New Roman",
        true,
        None::<&str>,
    )?;
    let font_courier_new =
        MenuItem::with_id(app, "font-courier-new", "Courier New", true, None::<&str>)?;
    let font_console = MenuItem::with_id(app, "font-console", "Console", true, None::<&str>)?;
    let font_family_submenu = Submenu::with_items(
        app,
        "字体风格",
        true,
        &[
            &font_arial,
            &font_helvetica,
            &font_georgia,
            &font_times_new_roman,
            &font_courier_new,
            &font_console,
        ],
    )?;

    // 倒计时菜单
    let countdown_cancel = MenuItem::with_id(app, "countdown-cancel", "取消", true, None::<&str>)?;
    let countdown_15s = MenuItem::with_id(app, "countdown-15s", "15秒", true, None::<&str>)?;
    let countdown_3m = MenuItem::with_id(app, "countdown-3m", "3分钟", true, None::<&str>)?;
    let countdown_5m = MenuItem::with_id(app, "countdown-5m", "5分钟", true, None::<&str>)?;
    let countdown_7m = MenuItem::with_id(app, "countdown-7m", "7分钟", true, None::<&str>)?;
    let countdown_10m = MenuItem::with_id(app, "countdown-10m", "10分钟", true, None::<&str>)?;
    let countdown_12m = MenuItem::with_id(app, "countdown-12m", "12分钟", true, None::<&str>)?;
    let countdown_15m = MenuItem::with_id(app, "countdown-15m", "15分钟", true, None::<&str>)?;
    let countdown_20m = MenuItem::with_id(app, "countdown-20m", "20分钟", true, None::<&str>)?;
    let countdown_30m = MenuItem::with_id(app, "countdown-30m", "30分钟", true, None::<&str>)?;
    let countdown_45m = MenuItem::with_id(app, "countdown-45m", "45分钟", true, None::<&str>)?;
    let countdown_1h = MenuItem::with_id(app, "countdown-1h", "1小时", true, None::<&str>)?;
    let countdown_submenu = Submenu::with_items(
        app,
        "倒计时",
        true,
        &[
            &countdown_cancel,
            &countdown_15s,
            &countdown_3m,
            &countdown_5m,
            &countdown_7m,
            &countdown_10m,
            &countdown_12m,
            &countdown_15m,
            &countdown_20m,
            &countdown_30m,
            &countdown_45m,
            &countdown_1h,
        ],
    )?;

    let menu = Menu::with_items(
        app,
        &[
            // &new_window_i,
            // &separator,
            &font_size_submenu,
            &font_color_submenu,
            &font_family_submenu,
            &separator,
            &countdown_submenu,
            &separator,
            &toggle_submenu,
            &help_i,
            &about_i,
            &quit_i,
        ],
    )?;
    Ok(menu)
}

/**
 * 处理菜单事件的函数
 * A function that handles menu events
 */
fn handle_menu_event<R: Runtime>(app: &AppHandle<R>, event_id: &str) {
    match event_id {
        "quit" => app.exit(0),
        // "new-window" => {
        //     create_webview_window(app, "new", "index.html", "").unwrap_or(());
        // }
        "font-14px" => {
            Emitter::emit(app, "change-font-size", "14px").unwrap_or(());
        }
        "font-16px" => {
            Emitter::emit(app, "change-font-size", "16px").unwrap_or(());
        }
        "font-18px" => {
            Emitter::emit(app, "change-font-size", "18px").unwrap_or(());
        }
        "font-20px" => {
            Emitter::emit(app, "change-font-size", "20px").unwrap_or(());
        }
        "font-24px" => {
            Emitter::emit(app, "change-font-size", "24px").unwrap_or(());
        }
        "font-36px" => {
            Emitter::emit(app, "change-font-size", "36px").unwrap_or(());
        }
        "font-48px" => {
            Emitter::emit(app, "change-font-size", "48px").unwrap_or(());
        }
        "font-60px" => {
            Emitter::emit(app, "change-font-size", "60px").unwrap_or(());
        }
        "font-72px" => {
            Emitter::emit(app, "change-font-size", "72px").unwrap_or(());
        }
        "font-ff4a58" => {
            Emitter::emit(app, "change-font-color", "#FF4A58").unwrap_or(());
        }
        "font-f7880d" => {
            Emitter::emit(app, "change-font-color", "#F7880D").unwrap_or(());
        }
        "font-ebb10d" => {
            Emitter::emit(app, "change-font-color", "#EBB10D").unwrap_or(());
        }
        "font-6dd400" => {
            Emitter::emit(app, "change-font-color", "#6DD400").unwrap_or(());
        }
        "font-05d892" => {
            Emitter::emit(app, "change-font-color", "#05D892").unwrap_or(());
        }
        "font-158aff" => {
            Emitter::emit(app, "change-font-color", "#158AFF").unwrap_or(());
        }
        "font-8b0dcb" => {
            Emitter::emit(app, "change-font-color", "#8B0DCB").unwrap_or(());
        }
        "font-arial" => {
            Emitter::emit(app, "change-font-family", "Arial").unwrap_or(());
        }
        "font-helvetica" => {
            Emitter::emit(app, "change-font-family", "Helvetica").unwrap_or(());
        }
        "font-georgia" => {
            Emitter::emit(app, "change-font-family", "Georgia").unwrap_or(());
        }
        "font-times-new-roman" => {
            Emitter::emit(app, "change-font-family", "Times New Roman").unwrap_or(());
        }
        "font-courier-new" => {
            Emitter::emit(app, "change-font-family", "Courier New").unwrap_or(());
        }
        "font-console" => {
            Emitter::emit(app, "change-font-family", "Console").unwrap_or(());
        }
        "countdown-cancel" => {
            Emitter::emit(app, "change-countdown", 0).unwrap_or(());
        }
        "countdown-15s" => {
            Emitter::emit(app, "change-countdown", 15).unwrap_or(());
        }
        "countdown-3m" => {
            Emitter::emit(app, "change-countdown", 180).unwrap_or(());
        }
        "countdown-5m" => {
            Emitter::emit(app, "change-countdown", 300).unwrap_or(());
        }
        "countdown-7m" => {
            Emitter::emit(app, "change-countdown", 420).unwrap_or(());
        }
        "countdown-10m" => {
            Emitter::emit(app, "change-countdown", 600).unwrap_or(());
        }
        "countdown-12m" => {
            Emitter::emit(app, "change-countdown", 720).unwrap_or(());
        }
        "countdown-15m" => {
            Emitter::emit(app, "change-countdown", 900).unwrap_or(());
        }
        "countdown-20m" => {
            Emitter::emit(app, "change-countdown", 1200).unwrap_or(());
        }
        "countdown-30m" => {
            Emitter::emit(app, "change-countdown", 1800).unwrap_or(());
        }
        "countdown-45m" => {
            Emitter::emit(app, "change-countdown", 2700).unwrap_or(());
        }
        "countdown-1h" => {
            Emitter::emit(app, "change-countdown", 3600).unwrap_or(());
        }

        "help-menu" => {
            create_webview_window(app, "help", "help.html", "帮助").unwrap_or(());
        }
        "about-menu" => {
            create_webview_window(app, "about", "about.html", "关于 hClock").unwrap_or(());
        }
        "show" => {
            if let Some(window) = app.get_webview_window("main") {
                window.show().unwrap_or(());
            }
        }
        "hide" => {
            if let Some(window) = app.get_webview_window("main") {
                window.hide().unwrap_or(());
            }
        }
        _ => {}
    }
}
